﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;

namespace MpSoft.Library.CommonLibrary
{
    public class ImageInfo
    {
        public static void SetImageSize(System.Web.UI.WebControls.Image imgBind, string pathImage, 
            string notFountImg, int maxWidth, int maxHeight)
        {
            try
            {
                int imageHeight = 0, imageWidth = 0;
                try
                {
                    FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(pathImage),
                      FileMode.Open, FileAccess.Read, FileShare.Read);
                    System.Drawing.Image imageToBeResized = System.Drawing.Image.FromStream(fs);
                    imageHeight = imageToBeResized.Height;
                    imageWidth = imageToBeResized.Width;
                }
                catch (System.Web.HttpException)
                {
                    imageHeight = maxHeight;
                    imageWidth = maxWidth;
                }
                if (imageWidth < maxWidth && imageHeight < maxHeight)
                {
                    imgBind.Width = imageWidth;
                    imgBind.Height = imageHeight;
                }
                else
                {
                    imageWidth = (maxHeight * imageWidth) / imageHeight;
                    imageHeight = imageHeight > maxHeight ? maxHeight : imageHeight;
                    if (imageWidth > maxWidth)
                    {
                        imageHeight = (maxWidth * imageHeight) / imageWidth;
                        imageWidth = maxWidth;
                    }
                    imgBind.Width = imageWidth;
                    imgBind.Height = imageHeight;
                }
            }
            catch (System.IO.FileNotFoundException)
            {
                imgBind.Width = maxWidth;
                imgBind.Height = maxHeight;
                imgBind.ImageUrl = notFountImg;
            }
            catch (System.IO.DirectoryNotFoundException)
            {
                imgBind.Width = maxWidth;
                imgBind.Height = maxHeight;
                imgBind.ImageUrl = notFountImg;
            }
            catch (System.UnauthorizedAccessException)
            {
                imgBind.Width = maxWidth;
                imgBind.Height = maxHeight;
                imgBind.ImageUrl = notFountImg;
            }
            catch (System.IO.IOException)
            {
                imgBind.Width = maxWidth;
                imgBind.Height = maxHeight;
                imgBind.ImageUrl = notFountImg;
            }
            catch (System.ArgumentException)
            {
                imgBind.Width = maxWidth;
                imgBind.Height = maxHeight;
                imgBind.ImageUrl = notFountImg;
            }
        }
        public static ImageSize Image(string pathImage, string notFountImg, int maxWidth, int maxHeight)
        {
            ImageSize objImage = new ImageSize();
            if (string.IsNullOrEmpty(pathImage))
                pathImage = notFountImg;
            try
            {
                int imageHeight = 0, imageWidth = 0;
                try
                {
                    FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(pathImage),
                      FileMode.Open, FileAccess.Read, FileShare.Read);
                    System.Drawing.Image imageToBeResized = System.Drawing.Image.FromStream(fs);
                    imageHeight = imageToBeResized.Height;
                    imageWidth = imageToBeResized.Width;
                }
                catch (System.Web.HttpException)
                {
                    imageHeight = maxHeight;
                    imageWidth = maxWidth;
                }
                if (imageWidth < maxWidth && imageHeight < maxHeight)
                {
                    objImage.Width = imageWidth;
                    objImage.Height = imageHeight;
                }
                else
                {
                    imageWidth = (maxHeight * imageWidth) / imageHeight;
                    imageHeight = imageHeight > maxHeight ? maxHeight : imageHeight;
                    if (imageWidth > maxWidth)
                    {
                        imageHeight = (maxWidth * imageHeight) / imageWidth;
                        imageWidth = (imageHeight * maxWidth) / maxHeight;
                    }
                    objImage.Width = imageWidth;
                    objImage.Height = imageHeight;
                }                
            }
            catch (System.IO.FileNotFoundException)
            {
                objImage.Width = maxWidth;
                objImage.Height = maxHeight;
            }
            catch (System.IO.DirectoryNotFoundException)
            {
                objImage.Width = maxWidth;
                objImage.Height = maxHeight;
            }
            catch (System.UnauthorizedAccessException)
            {
                objImage.Width = maxWidth;
                objImage.Height = maxHeight;
            }
            return objImage;
        }
    }
}
